using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace PhilWicklund.com.ListItemArchiver
{
	/// <summary>
	/// Description of the toolpart. Override the GetToolParts method in your WebPart
	/// class to invoke this toolpart. To establish a reference to the Web Part 
	/// the user has selected, use the ParentToolPane.SelectedWebPart property.
	/// </summary>
	public class ArchivedListItemDisplayerTP: Microsoft.SharePoint.WebPartPages.ToolPart
	{
		string defaultList;

		public ArchivedListItemDisplayerTP(string listName) 
		{
			defaultList = listName;
		}
		private DropDownList lists;

		/// <summary>
		/// Takes place when the "Apply" or "Ok" buttons are clicked.
		/// When this happens the the customizations in the toolpart are saved
		/// into the webpart.
		/// </summary>
		public override void ApplyChanges()
		{
			//Get a reference to the web part associated with this tool part
			ToolPane toolPane = this.ParentToolPane;
			ArchivedListItemDisplayerWP alidwp = (ArchivedListItemDisplayerWP) toolPane.SelectedWebPart;
			alidwp.ListName = lists.SelectedValue;
		}

		/// <summary>
		/// Creates and initializes the controls needed for this toolpart
		/// </summary>
		protected override void CreateChildControls()
		{
			try 
			{
				lists = new DropDownList();
			
				SPWeb web = SPControl.GetContextWeb(Context);
				foreach (SPList list in web.Lists)
				{ 
					lists.Items.Add(list.Title);
				}
				try {ListItem item = lists.Items.FindByText(defaultList); item.Selected = true;} 
				catch {}

				this.Controls.Add(new LiteralControl("Select List To Archive<br>"));
				this.Controls.Add(lists);
			} 
			catch (Exception e) 
			{
				this.Controls.Add(new LiteralControl("ERROR: " + e.Message));
			}
		}
	}											
}
								
